package com.whmnx.main.ui.model;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.tencent.qcloud.ugckit.utils.ToastUtil;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.main.R;
import com.whmnx.main.ui.MainActivity;
import com.whmnx.main.ui.activity.MessageActivity;
import com.whmnx.main.ui.fragment.LikeFragment;
import com.whmnx.main.ui.fragment.WorksFragment;
import com.whmnx.main.ui.videorecord.TCVideoRecordActivity;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/25 0025 17:42
 * version: 1.0
 * @author Administrator
 */
public class MainModel extends BaseViewModel {

    public ObservableField<String> quit = new ObservableField<>("全屏模式");
    private MainActivity mActivity;
    public MainModel(@NonNull Application application) {
        super(application);
    }

    private Disposable subscribe;


    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public void initToolbar(MainActivity mainActivity) {
        this.mActivity = mainActivity;
        initView();
    }

    private void initView() {
        subscribe = RxBus.getDefault().toObservable(Object.class)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object object){
                        if (object.equals(EventConstants.ALL3)){
                            quit.set("退出大屏模式");
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    public class UIChangeObservable {
        //左边观
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    }


    //中间
    public BindingCommand centerOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            int lv = SPUtils.getInstance().getInt(Constants.LV);
            if (lv == 0){
                ToastUtil.toastShortMessage("请先去V1认证");
                return;
            }
            Intent intent = new Intent(mActivity, TCVideoRecordActivity.class);
            mActivity.startActivity(intent);
            mActivity.overridePendingTransition(R.anim.push_top_in,
                    R.anim.push_top_out);
        }
    });

    //全屏
    public BindingCommand LeftOutOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.pSwitchEvent.setValue(false);
            if (quit.get().equals("全屏模式")){
                quit.set("退出大屏模式");
                RxBus.getDefault().post(EventConstants.ALL);
            }else {
                quit.set("全屏模式");
                RxBus.getDefault().post(EventConstants.ALL2);
            }
        }
    });
    //我的作品
    public BindingCommand OnClickCommand1 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(WorksFragment.class.getCanonicalName());
            uc.pSwitchEvent.setValue(false);
        }
    });
    //喜欢
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(LikeFragment.class.getCanonicalName());
            uc.pSwitchEvent.setValue(false);
        }
    });
    //新闻
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.pSwitchEvent.setValue(false);
        }
    });
    //游戏
    public BindingCommand OnClickCommand4 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
           // Fragment myGeneralFragment = (Fragment) ARouter.getInstance().build(RouterFragmentPath.User.MY_GUANLI).navigation();
           // RxBus.getDefault().post(EventConstants.FRIENDS);
            uc.pSwitchEvent.setValue(false);
        }
    });
    //消息
    public BindingCommand OnClickCommand5 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(MessageActivity.class);
            uc.pSwitchEvent.setValue(false);
        }
    });


    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
